package com.smartHire.controller.Business;


import com.alibaba.fastjson.JSONObject;
import com.smartHire.domain.Goods;
import com.smartHire.service.GoodsService;
import com.smartHire.util.HttpRequest;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "AuditServlet", value = "/AuditServlet")
public class AuditServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String good_id = request.getParameter("good_id");
        String status = request.getParameter("status");


        //String sad=


        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器对象的信息========" + ctx);
        //获取service


        try {

            GoodsService goodsServiceservice  = (GoodsService) ctx.getBean("goodsService");
            Goods G = new Goods();
            if(good_id==null){


                G.setAudit("未审核");

                List<Goods> goods = goodsServiceservice.queryGoodsByAudit(G);
                System.out.println(goods);
                String jsonStr = JSONObject.toJSONString(goods);

                response.setCharacterEncoding("utf-8");
                response.getWriter().write(jsonStr);
                System.out.println(jsonStr);

            }
            else if(good_id!=null & status.equals("fail")){
                G.setGood_id(Integer.parseInt(good_id));

                if(goodsServiceservice.deleteGoods(G)==1){
                    System.out.println("asdasdasdasdghjasghdiyuasghdui bgsyuiadgasyuidguiasgdhjkasdghjkasdgjkasghdkashdkjhasdjkasdsadasd");
                }
            }

            else if(good_id!=null & status.equals("suc")){
                G.setGood_id(Integer.parseInt(good_id));
                G.setAudit("已审核");
                if(goodsServiceservice.updateGoodsById(G)==1){
                    System.out.println("asdasdasdasdghjasghdiyuasghdui bgsyuiadgasyuidguiasgdhjkasdghjkasdgjkasghdkashdkjhasdjkasdsadasd");
                }
            }




        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
